package cn.wokoba.service.activity.service.trial.node;

import cn.wokoba.service.activity.model.entity.MarketProductEntity;
import cn.wokoba.service.activity.model.entity.TrialBalanceEntity;
import cn.wokoba.service.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.wokoba.service.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.wokoba.types.desgin.framework.tree.IStrategyHandler;
import cn.wokoba.types.enums.ResponseCode;
import cn.wokoba.types.exception.AppException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


//异常节点处理；无营销、流程降级、超时调用等，都可以路由到 ErrorNode 节点统一处理
@Service
@Slf4j
public class ErrorNode extends AbstractGroupBuyMarketSupport {
    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("拼团商品查询试算服务-NoMarketNode userId:{} requestParameter:{}", requestParameter.getUserId(), JSON.toJSONString(requestParameter));

        //人群过滤
        if (!(dynamicContext.isVisible() && dynamicContext.isEnable())) {
            log.error("人群标签过滤 activityId:{}  user:{}", requestParameter.getActivityId(), requestParameter.getUserId());
            throw new AppException(ResponseCode.TAG_REFUSE.getCode(), ResponseCode.TAG_REFUSE.getInfo());
        }
        // 无营销配置
        if (null == dynamicContext.getGroupBuyActivityDiscountVO() || null == dynamicContext.getSkuVO()) {
            log.error("商品无拼团营销配置 {}", requestParameter.getGoodsId());
            throw new AppException(ResponseCode.E0002.getCode(), ResponseCode.E0002.getInfo());
        }


        return TrialBalanceEntity.builder().build();

    }

    @Override
    public IStrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return null;
    }
}
